local lockey = KEYS[1]
local hashkey = KEYS[2]
local max = tonumber(ARGV[1])

local value = tonumber(redis.call('hget', lockey, hashkey))

if value >= max then
    redis.call('hset', lockey, hashkey, max)
elseif value <= 0 then
    redis.call('hdel', lockey, hashkey)
else
    redis.call('hset', lockey, hashkey, value - 1)
end
